/**
 * Copyright Dinglian. All Rights Reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lyh.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 类描述
 *
 * @author LinYeHui
 * @date 2022/5/26
 * @company Dinglian
 */

/**
 * 定义服务消费端Controller,在这个Controller对象
 * 的方法中实现对远端服务sca-provider的调用
 */

@RestController
public class ConsumerController {
    /**
     * 从spring容器获取一个RestTemplate对象,
     * 基于此对象实现远端服务调用
     */
    @Autowired
    private RestTemplate restTemplate;

    //@Autowired //推荐构造注入 (假如只有一个构造方法autowired也可以省略)
    //public ConsumerController(RestTemplate restTemplate){
    //    this.restTemplate=restTemplate;
    //}
    @Value("${server.port:8080}")
    private String serverPort;
    /**
     * 基于此对象可以从nacos中动态获取服务列表,
     * 并基于内置负载均衡算法获取服务实例
     */
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    /**
     * 定义服务调用,在此方法中我们通过restTemplate基于指定url调用远端服务
     * 在此方法中通过一个RestTemplate对象调用远端sca-provider中的服务
     * @return
     * 访问此方法的url: http://localhost:8090/consumer/doRestEcho1
     */
    @GetMapping("/consumer/doRestEcho1")
    public String doRestEcho01(){
        //1.定义要调用的远端服务的url
        String url="http://localhost:8081/provider/echo/8090";
        //2.基于restTemplate对象中的相关方法进行服务调用
        //return restTemplate.getForObject(url, String.class);
        return restTemplate.getForObject(url, String.class, serverPort);//String.class为url对应服务的响应结果类型
    }

    //http://ip:port/consumer/doRestEcho2
    @GetMapping("/consumer/doRestEcho2")
    public String doRestEcho02() {
        //方案1:自己写负载均衡算法
        //定义远端服务的url地址
        //String url1="http://localhost:8081/provider/echo/{msg}";
        //String url2="http://localhost:8082/provider/echo/{msg}";
        //String[] urlArray={url1,url2};
        //String url=urlArray[new Random().nextInt(urlArray.length)];
        //基于RestTemplate借助http协议向远端服务发起Get请求
        //return restTemplate.getForObject(url,
        //String.class,serverPort);//String.class为url对应服务的响应结果类型

        //方案2:从nacos中动态获取服务列表,并基于内置负载均衡算法获取服务实例
        //1)基于服务名(这个名字是nacos注册中心中的一个服务名)获取服务实例
        ServiceInstance serviceInstance = loadBalancerClient.choose("sca-provider");
        //2)基于服务实例构建远端服务url
        String url = "http://%s:%s/provider/echo/{msg}";
        String ip = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        url = String.format(url, ip, port);
        //3)进行远端服务调用
        return restTemplate.getForObject(url, String.class, serverPort);
    }

//    @Autowired
//    private RestTemplate loadBalancedRestTemplate;
//
//    @GetMapping("/consumer/doRestEcho3")
//    public String doRestEcho03(){
//        String url=String.format("http://%s/provider/echo/%s","sca-provider",appName);
//        //向服务提供方发起http请求,获取响应数据
//        return loadBalancedRestTemplate.getForObject(
//                url,//要请求的服务的地址
//                String.class);//String.class为请求服务的响应结果类型
//    }

}
